package trading.algo.BearTask;

import java.util.Timer;
import java.util.TimerTask;

import trading.algo.TimerKillerTask;
import trading.bo.IBAccessorBO;
import trading.util.MarketTimeHelper;
import trading.util.VIXFrontMonthCheckUtil;

public class VIXFutureCheckFrontMonthDateTask extends TimerTask {


	private MarketTimeHelper marketTimeHelper;
	private IBAccessorBO ibAccessorBO;
	
	public VIXFutureCheckFrontMonthDateTask(IBAccessorBO ibAccessorBO, MarketTimeHelper marketTimeHelper) {
		this.marketTimeHelper = marketTimeHelper;
	    this.ibAccessorBO = ibAccessorBO;
	}
	
	@Override
	public void run() {
		String frontMonthPrice = VIXFrontMonthCheckUtil.getVIXFrontMonthPrice();
		VIXFrontMonthCheckUtil.finishTestingFrontMonthDate();
		System.out.println("Finish testing VIX future front month date");

		Timer timer = new Timer();
		int taskID = 0;
		// Current month is not front month
		if (frontMonthPrice == null) {
			System.out.println("Current month is not VIX future front month.");
			VIXFuturePriceTask vixFuturePriceTask = new VIXFuturePriceTask(this.ibAccessorBO, this.marketTimeHelper.getNextMonthExpiration());
	        timer.schedule(vixFuturePriceTask, taskID * 1000);
	        taskID+=VIXFuturePriceTask.secondsNeeded();
	        
			VIXFuturePriceTask vixFuturePriceTask1 = new VIXFuturePriceTask(this.ibAccessorBO, this.marketTimeHelper.getNextNextMonthExpiration());
	        timer.schedule(vixFuturePriceTask1, taskID * 1000);
	        taskID+=VIXFuturePriceTask.secondsNeeded();
		} else {
			System.out.println("Current month is VIX future front month.");
			VIXFuturePriceTask.storeVIXFuturePrice(Double.parseDouble(frontMonthPrice));
	        
			VIXFuturePriceTask vixFuturePriceTask1 = new VIXFuturePriceTask(this.ibAccessorBO, this.marketTimeHelper.getNextMonthExpiration());
	        timer.schedule(vixFuturePriceTask1, taskID * 1000);
	        taskID+=VIXFuturePriceTask.secondsNeeded();
		}
		TimerKillerTask timerKillerTask = new TimerKillerTask(timer);
		timer.schedule(timerKillerTask, taskID * 1000);
		
	}
	
	public static int secondsNeeded() {
	    return VIXFuturePriceTask.secondsNeeded() + VIXFuturePriceTask.secondsNeeded();
	}
}
